home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / .bin / httpd / src / http_auth.c < prev    next >
C/C++ Source or Header  |  1995-05-18  |  4KB  |  135 lines

  1. /*
  2.  * http_auth: authentication
  3.  * 
  4.  * All code contained herein is covered by the Copyright as distributed
  5.  * in the README file in the main directory of the distribution of 
  6.  * NCSA HTTPD.
  7.  *
  8.  * Based on NCSA HTTPd 1.3 by Rob McCool
  9.  * 
  10.  */
  11.  
  12.  
  13. #include "httpd.h"
  14.  
  15. char user[MAX_STRING_LEN];
  16. char groupname[MAX_STRING_LEN];
  17.  
  18.  
  19. void auth_bong(char *s, FILE *out) {
  20.     char errstr[MAX_STRING_LEN];
  21.  
  22. /* debugging */
  23.     if(s) {
  24.         sprintf(errstr,"%s authorization: %s",remote_name,s);
  25.         log_error(errstr);
  26.     }
  27.     if(!strcasecmp(auth_type,"Basic")) {
  28.         sprintf(errstr,"Basic realm=\"%s\"",auth_name);
  29.         die(AUTH_REQUIRED,errstr,out);
  30.     }
  31.     else {
  32.         sprintf(errstr,"Unknown authorization method %s",auth_type);
  33.         die(SERVER_ERROR,errstr,out);
  34.     }
  35. }
  36.  
  37. void check_auth(security_data *sec, int m, FILE *out) {
  38.     char at[MAX_STRING_LEN];
  39.     char ad[MAX_STRING_LEN];
  40.     char sent_pw[MAX_STRING_LEN];
  41.     char real_pw[MAX_STRING_LEN];
  42.     char t[MAX_STRING_LEN];
  43.     char w[MAX_STRING_LEN];
  44.     char errstr[MAX_STRING_LEN];
  45.     register int x,y;
  46.     int grpstatus;
  47.  
  48.     if(!auth_type) {
  49.         sprintf(errstr,
  50. "httpd: authorization required for %s but not configured",sec->d);
  51.         die(SERVER_ERROR,errstr,out);
  52.     }
  53.  
  54.     if(!strcasecmp(auth_type,"Basic")) {
  55.         if(!auth_name) {
  56.             sprintf(errstr,"httpd: need AuthName for %s",sec->d);
  57.             die(SERVER_ERROR,errstr,out);
  58.         }
  59.         if(!auth_line[0])
  60.             auth_bong(NULL,out);
  61.         if(!auth_pwfile) {
  62.             sprintf(errstr,"httpd: need AuthUserFile for %s",sec->d);
  63.             die(SERVER_ERROR,errstr,out);
  64.         }
  65.         sscanf(auth_line,"%s %s",at,t);
  66.         if(strcmp(at,auth_type))
  67.             auth_bong("type mismatch",out);
  68.         uudecode(t,(unsigned char *)ad,MAX_STRING_LEN);
  69.         getword(user,ad,':');
  70.         strcpy(sent_pw,ad);
  71.         if(!get_pw(user,real_pw,out)) {
  72.             sprintf(errstr,"user %s not found",user);
  73.             auth_bong(errstr,out);
  74.         }
  75.         /* anyone know where the prototype for crypt is? */
  76.         if(strcmp(real_pw,(char *)crypt(sent_pw,real_pw))) {
  77.             sprintf(errstr,"user %s: password mismatch",user);
  78.             auth_bong(errstr,out);
  79.         }
  80.     }
  81.     else {
  82.         sprintf(errstr,"unknown authorization type %s for %s",auth_type,
  83.                 sec->d);
  84.         auth_bong(errstr,out);
  85.     }
  86.  
  87.     /* Common stuff: Check for valid user */
  88.     if(auth_grpfile)
  89.         grpstatus = init_group(auth_grpfile,out);
  90.     else
  91.         grpstatus = 0;
  92.  
  93.     for(x=0;x<sec->num_auth[m];x++) {
  94.         strcpy(t,sec->auth[m][x]);
  95.         getword(w,t,' ');
  96.         if(!strcmp(w,"valid-user"))
  97.             goto found;
  98.         if(!strcmp(w,"user")) {
  99.             while(t[0]) {
  100.                 if(t[0] == '\"') {
  101.                     getword(w,&t[1],'\"');
  102.                     for(y=0;t[y];y++)
  103.                         t[y] = t[y+1];
  104.                 }
  105.                 getword(w,t,' ');
  106.                 if(!strcmp(user,w))
  107.                     goto found;
  108.             }
  109.         }
  110.         else if(!strcmp(w,"group")) {
  111.             if(!grpstatus) {
  112.                 sprintf(errstr,"group required for %s, bad groupfile",
  113.                         sec->d);
  114.                 auth_bong(errstr,out);
  115.             }
  116.             while(t[0]) {
  117.                 getword(w,t,' ');
  118.                 if(in_group(user,w)) {
  119.             strcpy(groupname,w);
  120.                     goto found;
  121.         }
  122.             }
  123.         }
  124.         else
  125.             auth_bong("require not followed by user or group",out);
  126.     }
  127.     if(grpstatus) kill_group();
  128.     sprintf(errstr,"user %s denied",user);
  129.     auth_bong(errstr,out);
  130.   found:
  131.     if(grpstatus)
  132.         kill_group();
  133. }
  134.  
  135.